﻿using System;
using System.Collections.Generic;

namespace Gaea.CodeFirst
{
    public class ConvertType
    {
        public string ConvertMySqlType(IEnumerable<Attribute> attributes, string column_type)
        {

            if (column_type == "String" || column_type.Contains("varchar"))
            {
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("StringLength") != null)
                    {
                        var stringLength = (int)attribute.GetType().GetProperty("StringLength").GetValue(attribute);
                        column_type = "varchar(" + stringLength + ")";
                    }
                    if (attribute.GetType().GetProperty("StringDefault") != null)
                    {
                        var stringDefault = (string)attribute.GetType().GetProperty("StringDefault").GetValue(attribute);
                        column_type = column_type + " default " + stringDefault;
                    }
                }
            }
            else if (column_type == "Int32" || column_type.Contains("int"))
            {
                column_type = "int";
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("IntDefault") != null)
                    {
                        var intDefault = (int)attribute.GetType().GetProperty("IntDefault").GetValue(attribute);
                        column_type = "int default " + intDefault;
                    }
                }
            }
            else if (column_type == "Int64" || column_type.Contains("bigint"))
            {
                column_type = "bigint";
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("IntDefault") != null)
                    {
                        var intDefault = (int)attribute.GetType().GetProperty("IntDefault").GetValue(attribute);
                        column_type = "bigint default " + intDefault;
                    }
                }
            }

            return column_type;
        }

        public string ConvertSqlServerType(IEnumerable<Attribute> attributes, string column_type)
        {
            if (column_type == "String" || column_type.Contains("varchar"))
            {
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("StringLength") != null)
                    {
                        var stringLength = (int)attribute.GetType().GetProperty("StringLength").GetValue(attribute);
                        column_type = "varchar(" + stringLength + ")";
                    }
                    if (attribute.GetType().GetProperty("StringDefault") != null)
                    {
                        var stringDefault = (string)attribute.GetType().GetProperty("StringDefault").GetValue(attribute);
                        column_type = column_type + " default " + stringDefault;
                    }
                }
            }
            else if (column_type == "Int32" || column_type.Contains("int"))
            {
                column_type = "int";
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("IntDefault") != null)
                    {
                        var intDefault = (int)attribute.GetType().GetProperty("IntDefault").GetValue(attribute);
                        column_type = "int default " + intDefault;
                    }
                }
            }
            else if (column_type == "Int64" || column_type.Contains("bigint"))
            {
                column_type = "bigint";
                foreach (var attribute in attributes)
                {
                    if (attribute.GetType().GetProperty("IntDefault") != null)
                    {
                        var intDefault = (int)attribute.GetType().GetProperty("IntDefault").GetValue(attribute);
                        column_type = "bigint default " + intDefault;
                    }
                }
            }
            return column_type;
        }

        public string ConvertOracleType(IEnumerable<Attribute> attributes, string column_type)
        {

            return column_type;
        }
    }
}
